<template>
  <div class="app-container" v-loading="loading">
    <el-dialog title="设置考试培训时间" :visible.sync="dialogShow">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="考试名称" prop="examinationActiveName">
          <el-input type="text" v-model="form.active_name" auto-complete="off" :disabled="true"></el-input>
          <el-input type="hidden" v-model="form.active_id" auto-complete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="培训时间" prop="train_time">
          <el-col>
            <el-date-picker type="datetime" format="yyyy-MM-dd HH:mm:ss" placeholder="选择时间" v-model="form.train_time" style="width: 100%;"></el-date-picker>
          </el-col>
        </el-form-item>
        <center>
            <el-button type="primary" @click="onSubmit('form')">提交</el-button>
        </center>
      </el-form>
    </el-dialog>
    <el-table :data="examinationActive" v-loading="loading"
      style="width:100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection">
      </el-table-column>
      <el-table-column prop="ACTIVE_NAME" align="center" label="考试名称">
      </el-table-column>
            <el-table-column prop="IS_TRAIN_TIME_SETTING" align="center" label="培训时间是否设置">
              <template slot-scope="scope">
                <el-tag :type="scope.row.IS_TRAIN_TIME_SETTING === 1 ? 'success' : 'error' " close-transition>
                  {{scope.row.IS_TRAIN_TIME_SETTING === 1 ? '是':'否' }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="TRAIN_TIME" align="center" label="培训时间">
            </el-table-column>
            <el-table-column align="center" label="操作">
              <template slot-scope="scope">
                <el-button @click="handleClick(scope.row)" type="text">设置培训时间</el-button>
              </template>
            </el-table-column>
        </el-table>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getExaminationActive, settingTrainTime, getExaminationActiveTime } from '@/api/trainManagement'
import { parseTime } from '@/utils'
import _ from 'lodash'
export default {
  name: 'publishActive',
  data() {
    return {
      form: {
        train_time: '',
        active_id: '',
        active_name: ''
      },
      disabled: true,
      dialogShow: false,
      examinationActive: [],
      exam_start_time: '',
      multipleSelection: [],
      multipleSelectionIds: '',
      rules: {
        train_time: [
          { required: true, message: '请选择培训时间', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'roles'
    ])
  },
  created: function() {
    this.loading = true
    const param = {
      'username': this.name
    }
    getExaminationActive(param.username).then(response => {
      this.examinationActive = response.result
      this.loading = false
    }).catch(error => {
      console.log(error)
      this.loading = false
      this.$message.error('获取考试名称错误')
    })
  },
  methods: {
    onSubmit(formName) {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true
          // const curTime = new Date()
          const param = {
            'active_id': this.form.active_id,
            'train_time': parseTime(this.form.train_time)
          }
          settingTrainTime(param.active_id, param.train_time).then(response => {
            this.loading = false
            this.queryExaminationActive()
          }).catch(error => {
            console.log(error)
            this.loading = false
            this.$message.error('数据提交失败,请联系管理员')
          })
        } else {
          console.log('error')
        }
      })
    },
    queryExaminationActive: function() {
      this.loading = true
      const param = {
        'username': this.name
      }
      getExaminationActive(param.username).then(response => {
        this.examinationActive = response.result
        this.loading = false
      }).catch(error => {
        console.log(error)
        this.loading = false
        this.$message.error('获取考试名称错误')
      })
    },
    getExaminationActiveTime: function(ele) {
      const param = {
        'active_id': ele
      }
      getExaminationActiveTime(param.active_id).then(response => {
        this.exam_start_time = response.result[0].EXAM_START_TIME
        // console.log(this.exam_start_time)
      }).catch(error => {
        console.log(error)
        this.loading = false
        this.$message.error('数据提交失败,请联系管理员')
      })
    },
    handleSelectionChange: function(val) {
      const that = this
      that.multipleSelection = []
      let ids = ''
      _.each(val, function(value, key) {
        that.multipleSelection.push(value.ACTIVE_ID)
        that.form.active_id = value.ACTIVE_ID
        ids += value.ACTIVE_ID + ','
      })
      that.multipleSelectionIds = (ids.substring(ids.length - 1) === ',') ? ids.substring(0, ids.length - 1) : ids
    },
    handleClick(val) {
      // console.log(val)
      this.form.active_id = val.ACTIVE_ID
      this.form.active_name = val.ACTIVE_NAME
      if (val.TRAIN_TIME == null) {
        this.form.train_time = ''
      } else {
        this.form.train_time = new Date(Date.parse(val.TRAIN_TIME.replace(/-/g, '/')))
      }
      this.dialogShow = true
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.line{
  text-align: center;
}
</style>

